//
//  colorFuncGroup.swift
//  demo01
//
//  Created by babybus on 2025/7/1.
//
import Foundation
import UIKit
import SnapKit

class ColorFuncGroupStack:UIStackView{
    //全局变量
    private var layoutButton:MyButton!
    private var colorButton:MyButton!
    private var addTotailButton:MyButton!
    private var addToRandomButton:MyButton!
    
    //初始化方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        attributeInit()
        setupUI()
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:NSCoder) -- fatalError")
    }
    
    //属性配置
    func attributeInit(){
        axis = .horizontal
        spacing = 8
        alignment = .fill
        distribution = .fillEqually
    }
    func configure(action:Selector){
        layoutButton.addTarget(self, action: action, for: .touchUpInside)
    }
    //布局配置
    func setupUI(){
        layoutButton = MyButton()
        colorButton = MyButton()
        addTotailButton = MyButton()
        addToRandomButton = MyButton()
        
        layoutButton.configure(title: "按钮1", titleFor: .normal,titleColor: .black, titleColorFor: .normal,backgroundColor: .green)
        layoutButton.addTarget(self, action: #selector(changeLayout), for: .touchUpInside)
        
        colorButton.configure(title: "按钮2", titleFor: .normal,titleColor: .black, titleColorFor: .normal,backgroundColor: .blue)
        colorButton.addTarget(self, action: #selector(changecolor), for: .touchUpInside)
        
        addTotailButton.configure(title: nil, titleFor: .normal,titleColor: .black, titleColorFor: .normal,backgroundColor: .yellow)
        addTotailButton.setImage(UIImage(systemName: "plus"), for: .normal)
        addTotailButton.addTarget(self, action: #selector(addTotail), for: .touchUpInside)
        
        addToRandomButton.configure(title: nil, titleFor: .normal, titleColor: .black, titleColorFor: .normal, backgroundColor: .orange)
        addToRandomButton.setImage(UIImage(systemName: "minus"), for: .normal)
        addToRandomButton.addTarget(self, action: #selector(addToRandom), for: .touchUpInside)
        
        [layoutButton, colorButton, addTotailButton, addToRandomButton].forEach {
            self.addArrangedSubview($0)
            $0.snp.makeConstraints { make in
                make.height.equalTo(50)
            }
        }
    }
    
    //额外函数
    @objc func changeLayout(){
        print("changeLayout")
//        if numsOfRow == 3{
//            numsOfRow = 1
//        }else {
//            numsOfRow = 3
//        }
//        collectionView.reloadData()
    }
    @objc func changecolor(number:Int){
        print("changecolor")
//        let randomIndices = Array(0..<number).shuffled().prefix(3)
//        for i in randomIndices{
//            items[i].ColorType = getcolor()
//        }
//        collectionView.reloadData()
    }
    @objc func getcolor()->UIColor{
        let red = CGFloat.random(in: 0...1)
        let green = CGFloat.random(in: 0...1)
        let blue = CGFloat.random(in: 0...1)
        return UIColor(red:red,green:green,blue:blue,alpha: 1.0)
    }
    
    func getNewColorBlock()->ColorInfo{
        let randomColor = getcolor()
        let itemTemp = ColorInfo(ColorName: "随机", ColorType: randomColor, showBorder: false)
        return itemTemp
    }
    @objc func addTotail(){
        print("addTotail")
//        items.append(getNewColorBlock())
//        collectionView.reloadData()
    }
    
    @objc func addToRandom(){
        print("addToRandom")
//        let position = Int.random(in: 0..<items.count)
//        let block = getNewColorBlock()
//        items.insert(block, at: position)
//        collectionView.reloadData()
    }
}
